package com.java110.community.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.community.dao.IRepairServiceDao;
import com.java110.core.base.dao.BaseServiceDao;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairPhotoDto;
import com.java110.dto.repair.RepairReturnVisitDto;
import com.java110.dto.repair.RepairStaffsFlow;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("repairServiceDaoImpl")
public class RepairServiceDaoImpl extends BaseServiceDao implements IRepairServiceDao {
    private static Logger logger = LoggerFactory.getLogger(CommunityServiceDaoImpl.class);

    @Override
    public Long saveOwnerRepair(RepairDto repairDto) {
        sqlSessionTemplate.insert("repairServiceDaoImpl.saveOwnerRepair", repairDto);
        return repairDto.getId();
    }

    @Override
    public List<RepairDto> listOwnerRepairs(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        return sqlSessionTemplate.selectList("repairServiceDaoImpl.listOwnerRepairs", map);
    }

    @Override
    public void updateOwnerRepair(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        map.put("updateTime", DateUtil.getCurrentDate());
        Object state = map.get("state");
        if (state != null && state.equals("1600")) {
            map.put("commitTime", DateUtil.getCurrentDate());
        }
        sqlSessionTemplate.update("repairServiceDaoImpl.updateOwnerRepair", map);
    }

    @Override
    public void deleteOwnerRepair(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        sqlSessionTemplate.update("repairServiceDaoImpl.deleteOwnerRepair", map);
    }

    @Override
    public void repairDispatch(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        map.put("updateTime", DateUtil.getCurrentDate());
        sqlSessionTemplate.update("repairServiceDaoImpl.repairDispatch", map);
    }

    @Override
    public RepairDto getOneById(Long repairId) {
        return sqlSessionTemplate.selectOne("repairServiceDaoImpl.getOneById", repairId);
    }

    @Override
    public void repairDispatchFlow(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        map.put("creatTime", DateUtil.getCurrentDate());
        sqlSessionTemplate.insert("repairServiceDaoImpl.repairDispatchFlow", map);
    }

    @Override
    public List<RepairStaffsFlow> listRepairStaffs(JSONObject reqJson) {
        Long repairId = reqJson.getLong("repairId");

        return sqlSessionTemplate.selectList("repairServiceDaoImpl.listRepairStaffs", repairId);
    }

    @Override
    public RepairStaffsFlow lastTimeRepairFlow(Long repairId) {
        return sqlSessionTemplate.selectOne("repairServiceDaoImpl.lastTimeRepairFlow", repairId);
    }

    @Override
    public void updateRepairDispatchFlow(RepairStaffsFlow repairStaffsFlows) {
        sqlSessionTemplate.update("repairServiceDaoImpl.updateRepairDispatchFlow", repairStaffsFlows);
    }

    @Override
    public List<RepairDto> listStaffRepairs(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        return sqlSessionTemplate.selectList("repairServiceDaoImpl.listStaffRepairs", map);
    }

    @Override
    public void repairStop(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        map.put("updateTime", DateUtil.getCurrentDate());
        map.put("state", "1300");
        map.put("stateName", "暂停");
        sqlSessionTemplate.update("repairServiceDaoImpl.repairStop", map);
    }

    @Override
    public void repairStart(JSONObject reqJson) {
        Map<String, Object> map = reqJson.getInnerMap();
        map.put("updateTime", DateUtil.getCurrentDate());
        map.put("state", "1100");
        map.put("stateName", "接单");
        sqlSessionTemplate.update("repairServiceDaoImpl.repairStart", map);
    }

    @Override
    public void repairFinish(List<RepairPhotoDto> list) {
        sqlSessionTemplate.insert("repairServiceDaoImpl.repairFinish", list);
    }

    @Override
    public void choosedGoodsList(List<HashMap<Object, Object>> list) {
        sqlSessionTemplate.insert("repairServiceDaoImpl.choosedGoodsList", list);
    }

    @Override
    public List<RepairDto> queryRepairReturnVisit(String communityId) {
        return sqlSessionTemplate.selectList("repairServiceDaoImpl.queryRepairReturnVisit", communityId);
    }

    @Override
    public void saveRepairReturnVisit(RepairReturnVisitDto dto) {
        Map<String,Object> map = BeanConvertUtil.beanCovertMap(dto);
        map.put("creatTime", DateUtil.getCurrentDate());
        sqlSessionTemplate.insert("repairServiceDaoImpl.saveRepairReturnVisit", map);
    }
}
